package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationResDTO;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {

    @Resource
    private WorkerCertificationAuditMapper workerCertificationAuditMapper;
    @Resource
    private WorkerCertificationMapper workerCertificationMapper;


    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    /**
     * 提交认证申请
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAudit(WorkerCertificationAuditAddReqDTO dto) {
        // 获取服务人员的id
        Long userId = UserContext.currentUserId();
        // 判断是否是第一次申请
        WorkerCertification worker = workerCertificationMapper.selectById(userId);
        if (ObjectUtil.isNotEmpty(worker)) {
            workerCertificationMapper.selectById(worker.getId());
        }

        WorkerCertification workerCertification = BeanUtil.toBean(dto, WorkerCertification.class);
        workerCertification.setId(userId);
        workerCertification.setCertificationTime(LocalDateTime.now());
        workerCertification.setCertificationStatus(1);
        // 添加数据到服务人员认证信息表中
        super.save(workerCertification);
        // 添加数据到服务人员认证审核表中
        WorkerCertificationAudit workerCertificationAudit = BeanUtil.toBean(workerCertification, WorkerCertificationAudit.class);
        workerCertificationAudit.setServeProviderId(userId);
        workerCertificationAudit.setAuditStatus(0);
        workerCertificationAudit.setCertificationStatus(1);
        workerCertificationAuditMapper.insert(workerCertificationAudit);
    }

    /**
     * 查询最新的驳回原因
     *
     * @return
     */
    @Override
    public RejectReasonResDTO rejectReason() {
        Long userId = UserContext.currentUserId();
        LambdaQueryWrapper<WorkerCertificationAudit> wrapper = new QueryWrapper<WorkerCertificationAudit>().lambda()
                .eq(WorkerCertificationAudit::getServeProviderId, userId);
        WorkerCertificationAudit workerCertificationAudit = workerCertificationAuditMapper.selectOne(wrapper);
        if (workerCertificationAudit == null) {
            throw new ForbiddenOperationException("未查询到该审核表信息");
        }
        String rejectReason = workerCertificationAudit.getRejectReason();
        return new RejectReasonResDTO(rejectReason);

    }

    /**
     * 审核服务人员认证分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult<WorkerCertificationAudit> queryWorkerByPage(WorkerCertificationAuditPageQueryReqDTO dto) {
        // 审核服务人员认证分页查询
        Page<WorkerCertificationAudit> page = PageUtils.parsePageQuery(dto, WorkerCertificationAudit.class);
        LambdaQueryWrapper<WorkerCertificationAudit> wrapper = Wrappers.<WorkerCertificationAudit>lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(dto.getName()),WorkerCertificationAudit::getName,dto.getName())
                .eq(ObjectUtil.isNotEmpty(dto.getIdCardNo()),WorkerCertificationAudit::getIdCardNo,dto.getIdCardNo())
                .eq(ObjectUtil.isNotEmpty(dto.getAuditStatus()),WorkerCertificationAudit::getAuditStatus,dto.getAuditStatus())
                .eq(ObjectUtil.isNotEmpty(dto.getCertificationStatus()),WorkerCertificationAudit::getCertificationStatus,dto.getCertificationStatus());
        Page<WorkerCertificationAudit> certificationAuditPage = workerCertificationAuditMapper.selectPage(page, wrapper);
        // 封装数据
        return PageUtils.toPage(certificationAuditPage,WorkerCertificationAudit.class);
    }
}
